package ruler.services;

import ruler.main.R;
import ruler.main.WifiGPSRulerActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class RulerService extends Service {

	final static int myID = 1234;
	private final IBinder rulerServiceBinder = new RulerServiceBinder(this);
	private static final String RULER_SERVICE_TAG = "RulerService";
	private LockBroadcastReceiver screenLockBroadcastReceiver;
	private RulerServiceThread serviceThread;

	public void onCreate() {
		super.onCreate();
		this.serviceThread = new RulerServiceThread(this);
		this.serviceThread.start();
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		screenLockBroadcastReceiver = new LockBroadcastReceiver(this);
		registerReceiver(screenLockBroadcastReceiver, filter);
		Log.v(RULER_SERVICE_TAG,
				getResources().getString(R.string.serviceCreated));
	}

	public void onStart(Intent intent, int startid) {
		super.onStart(intent, startid);
		startOnForeground();
		Log.v(RULER_SERVICE_TAG,
				getResources().getString(R.string.serviceStarted));
	}

	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(screenLockBroadcastReceiver);
		if (serviceThread != null)
			serviceThread.requestStop();
		stopForeground(true);
		Log.v(RULER_SERVICE_TAG,
				getResources().getString(R.string.serviceStopped));
	}
	
	public void pause(){
		if (serviceThread != null && serviceThread.isAlive())
			serviceThread.requestStop();
		stopForeground(true);
	}
	
	public void resume(){
		serviceThread = new RulerServiceThread(this);
		serviceThread.start();
		startOnForeground();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return rulerServiceBinder;
	}

	private void startOnForeground() {
		startForeground(
				myID,
				createNotification(getResources().getString(
						R.string.serviceStarted)));
	}

	public void restartService() {
		if (serviceThread != null)
			serviceThread.requestStop();
		serviceThread = new RulerServiceThread(this);
		serviceThread.start();
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.notify(myID,
				createNotification(getResources().getString(R.string.serviceRestarted)));
	}

	private Notification createNotification(CharSequence tickerText) {
		Intent intent = new Intent(this, WifiGPSRulerActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent pendIntent = PendingIntent
				.getActivity(this, 0, intent, 0);
		Notification notification = new Notification(R.drawable.ic_launcher,
				tickerText, 10000);
		notification.setLatestEventInfo(this,
				getResources().getString(R.string.app_name), getResources()
						.getString(R.string.serviceDescription), pendIntent);
		return notification;
	}

}